$(function () {
    layui.use('form', function(){
        var form = layui.form;



        /* 部门职位联动部分 */
        if($("#ajax-department").length > 0) {
            var departmentObj = $("#ajax-department");
            var _code = departmentObj.attr('data-code');
            ajaxLoad(departmentObj, departmentObj.attr('data-url'), _code, 'id', 'department_name', function () {
                form.render('select');
            }, false);

            form.on('select(ajax-department)', function (data) {
                /*市联动 */
                if ($("#ajax-position").length > 0) {
                    $("#ajax-position").empty();
                    if (data.value != undefined && data.value != '' && data.value != 0) {
                        var positionObj = $('#ajax-position'),
                            _code = positionObj.attr('data-code'),
                            _url = positionObj.attr('data-url');
                        ajaxLoad(positionObj, _url + "/" + data.value, _code, 'id', 'position_name', function () {
                            form.render('select');
                        }, true);
                    }
                }
            });
        }

        /* 职位加载 */
        if($("#ajax-position").length > 0) {
            var positionObj = $('#ajax-position'),
                _code = positionObj.attr('data-code'),
                _fcode = positionObj.attr('data-fcode'),
                _url = positionObj.attr('data-url');
            if(_code != undefined && _code != '' && _code != 0){
                ajaxLoad(positionObj,_url+"/"+_fcode,_code,'id','position_name',function(){
                    form.render('select');
                },true);
            }
        }







        /* 省市区联动部分 */
        if($("#ajax-province").length > 0) {
            var provinceObj = $("#ajax-province");
            var _code = provinceObj.attr('data-code');
            ajaxLoad(provinceObj, provinceObj.attr('data-url'), _code, 'code', 'name', function () {
                form.render('select');
            }, false);

            form.on('select(ajax-province)', function (data) {
                /*市联动 */
                if ($("#ajax-city").length > 0) {
                    $("#ajax-city").empty();
                    if (data.value != undefined && data.value != '' && data.value != 0) {
                        var cityObj = $('#ajax-city'),
                            _code = cityObj.attr('data-code'),
                            _url = cityObj.attr('data-url');
                        ajaxLoad(cityObj, _url + "/" + data.value, _code, 'code', 'name', function () {
                            form.render('select');
                        }, true);
                    }
                }
                if ($("#ajax-area").length > 0) {
                    $("#ajax-area").empty();
                }
            });
        }

        /* 市选项加载 */
        if($("#ajax-city").length > 0) {
            var cityObj = $('#ajax-city'),
                _code = cityObj.attr('data-code'),
                _fcode = cityObj.attr('data-fcode'),
                _url = cityObj.attr('data-url');
            if(_code != undefined && _code != '' && _code != 0){
                ajaxLoad(cityObj,_url+"/"+_fcode,_code,'code','name',function(){
                    form.render('select');
                },true);
            }

            form.on('select(ajax-city)', function (data) {
                if ($("#ajax-area").length > 0) {
                    if (data.value != undefined && data.value != '' && data.value != 0) {
                        var areaObj = $('#ajax-area'),
                            _code = areaObj.attr('data-code'),
                            _url = areaObj.attr('data-url');
                        ajaxLoad(areaObj, _url + "/" + data.value, _code, 'code', 'name', function () {
                            form.render('select');
                        }, true);
                    }
                }
            });
        }

        /* 区选项加载 */
        if($("#ajax-area").length > 0) {
            var areaObj = $('#ajax-area'),
                _code = areaObj.attr('data-code'),
                _fcode = areaObj.attr('data-fcode'),
                _url = areaObj.attr('data-url');
            if(_code != undefined && _code != '' && _code != 0){
                ajaxLoad(areaObj, _url + "/" + _fcode, _code, 'code', 'name', function () {
                    form.render('select');
                }, true);
            }
        }



        if($("#layui-form").length > 0) {
            form.verify({
                video: function(value, item){ //value：表单的值、item：表单的DOM对象
                    if(value == undefined || value == ''){
                        return '请上传视频';
                    }
                },
                picurl: function(value, item){ //value：表单的值、item：表单的DOM对象
                    if(value == undefined || value == ''){
                        return '请上传图片';
                    }
                },
                photo: function(value, item){ //value：表单的值、item：表单的DOM对象
                    if(value == undefined || value == ''){
                        return '请上传图片';
                    }
                },
                file: function(value, item){ //value：表单的值、item：表单的DOM对象
                    if(value == undefined || value == ''){
                        return '请上传文件';
                    }
                },
                apk: function(value, item){ //value：表单的值、item：表单的DOM对象
                    if(value == undefined || value == ''){
                        return '请上传APK';
                    }
                }
            });

            /* 表单提交 */
            form.on('submit(layui-form-submit)', function (data) {
                $.ajax({
                    url: $(data.form).attr('action')
                    , type: 'POST'
                    , dataType: 'json'
                    , data: data.field
                    , beforeSend:function(){
                        layer.load(2);
                    }
                    , success: function (ret) {
                        if (ret.hasOwnProperty('code') && ret.code == 200) {
                            layer.msg(ret.message,{icon: 1,time:2000}, function(){
                                if(ret.redirect != 'parent') {
                                    if(ret.redirect == 'close'){
                                        window.opener=null;
                                        window.open('','_self');
                                        window.close();
                                    }else if(ret.redirect == 'current'){
                                        location.reload()
                                    }else{
                                        location.href = ret.redirect;
                                    }
                                }else{
                                    location.href = document.referrer;
                                }
                            });
                            return false;
                        }

                        layer.msg(ret.message,{icon:2});
                    }
                    , complete: function(){
                        layer.closeAll('loading');
                    }
                });
                return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
            });
        }
    });
});

function ajaxLoad(obj,url,id,_id,_name,_func,clear){
    if(clear === true) obj.empty();/*判断是否需要清空option*/
    $.ajax({
        url: url
        , type: 'POST'
        , dataType: 'json'
        , success: function (ret) {
            if (ret.hasOwnProperty('code') && ret.code == 200) {
                var data = ret.data;
                obj.append('<option value=""></option>');
                for (var i = 0; i < data.length; i++) {
                    var checkedStr = '';
                    if (id == data[i][_id]) {
                        checkedStr = 'selected';
                    }
                    var strHtml = '<option value="' + data[i][_id] + '" ' + checkedStr + '>' + data[i][_name] + '</option>';
                    obj.append(strHtml);
                }
                if(typeof _func === "function"){
                    //调用它，既然我们已经确定了它是可调用的
                   _func();
                }
            }
        }
    });
}
